//
//  CustomTravView.m
//  YouGoWeGo
//
//  Created by LiuFei on 14/11/21.
//  Copyright (c) 2014年 LiuFei. All rights reserved.
//

#import "CustomTravView.h"

#import "TravelModel.h"
@implementation CustomTravView

- (void)dealloc
{
    [_nameLabel release];
    [_recommended_reason release];
    [_tel release];
    [_telLabel release];
    [_timeLabel release];
    [_opening_time release];
    [_webLabel release];
    [_website release];
    [_address release];
    [_adLabel release];
    [_fee release];
    [_priceLabel release];
    [_arrival_type release];
    [_arrLabel release];
    [_descriptions release];
    [self removeObserver:self forKeyPath:@"model"];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.recommended_reason = [[UILabel alloc] initWithFrame:CGRectZero];
        [_recommended_reason setFont:[UIFont systemFontOfSize:18]];
        [self addSubview:_recommended_reason];
        [_recommended_reason release];
        
        self.descriptions = [[UILabel alloc] initWithFrame:CGRectZero];
        [_descriptions setNumberOfLines:0];
        [_descriptions setFont:[UIFont systemFontOfSize:16]];
        [self addSubview:_descriptions];
        [_descriptions release];
        
        self.adLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        [_adLabel setText:@"详细地址"];
        [self addSubview:_adLabel];
        [_adLabel release];
        self.address = [[UILabel alloc] initWithFrame:CGRectZero];
        [self addSubview:_address];
        [_address release];

        self.arrLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        [_arrLabel setText:@"到达方式"];
        [self addSubview:_arrLabel];
        [_arrLabel release];
        self.arrival_type = [[UILabel alloc] initWithFrame:CGRectZero];
        [self addSubview:_arrival_type];
        [_arrival_type release];
        
        self.timeLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        [_timeLabel setText:@"开放时间"];
        [self addSubview:_timeLabel];
        [_timeLabel release];
        self.opening_time = [[UILabel alloc] initWithFrame:CGRectZero];
        [self addSubview:_opening_time];
        [_opening_time release];
        
        self.priceLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        [_priceLabel setText:@"门票价格"];
        [self addSubview:_priceLabel];
        [_arrLabel release];
        self.fee = [[UILabel alloc] initWithFrame:CGRectZero];
        [self addSubview:_fee];
        [_fee release];
        
        self.telLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        [_telLabel setText:@"电话"];
        [self addSubview:_telLabel];
        [_fee release];
        self.tel = [[UILabel alloc] initWithFrame:CGRectZero];
        [self addSubview:_tel];
        [_tel release];
        
        self.webLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        [_webLabel setText:@"官方网站"];
        [self addSubview:_webLabel];
        [_webLabel release];
        self.website = [[UILabel alloc] initWithFrame:CGRectZero];
        [self addSubview:_website];
        [_website release];
        
        [self addObserver:self forKeyPath:@"model" options:NSKeyValueObservingOptionNew context:nil];
        
    }
    return self;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"model"]) {
        
        self.tel.text = self.model.tel;
        self.recommended_reason.text = self.model.recommended_reason;
        self.opening_time.text = self.model.opening_time;
        self.website.text = self.model.website;
        self.address.text = self.model.address;
        self.nameLabel.text = self.model.name;
        self.arrival_type.text = self.model.arrival_type;
        self.fee.text = self.model.fee;
        
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [_recommended_reason setFrame:CGRectMake(20, 0, [UIScreen mainScreen].bounds.size.width - 20, 30)];
    
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16], NSFontAttributeName, nil];
    CGRect rect = [_descriptions.text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 10, 500) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    [_descriptions setTextColor:[UIColor blackColor]];
    [_descriptions setFrame:CGRectMake(10, _recommended_reason.frame.size.height + 20, [UIScreen mainScreen].bounds.size.width - 20, rect.size.height)];
    
    [_adLabel setFrame:CGRectMake(10, self.descriptions.frame.size.height + self.descriptions.frame.origin.y + 20, [UIScreen mainScreen].bounds.size.width - 20, 20)];
    
    [_address setFrame:CGRectMake(10, self.adLabel.frame.size.height + 10 + self.adLabel.frame.origin.y, [UIScreen mainScreen].bounds.size.width - 20, 30)];
    
    [_arrLabel setFrame:CGRectMake(10, self.address.frame.size.height + 10 + self.address.frame.origin.y, [UIScreen mainScreen].bounds.size.width, 20)];

    [_arrival_type setFrame:CGRectMake(10, self.arrLabel.frame.size.height + 10 + self.arrLabel.frame.origin.y, [UIScreen mainScreen].bounds.size.width, 30)];
 
    [_timeLabel setFrame:CGRectMake(10, self.arrival_type.frame.size.height + 10 + self.arrival_type.frame.origin.y, [UIScreen mainScreen].bounds.size.width, 20)];
    
    [_opening_time setFrame:CGRectMake(10, self.timeLabel.frame.size.height + 10 + self.timeLabel.frame.origin.y, [UIScreen mainScreen].bounds.size.width, 30)];
    
    [_priceLabel setFrame:CGRectMake(10, self.opening_time.frame.size.height + 10 + self.opening_time.frame.origin.y, [UIScreen mainScreen].bounds.size.width, 20)];
    
    [_fee setFrame:CGRectMake(10, self.priceLabel.frame.size.height + 10 + self.priceLabel.frame.origin.y, [UIScreen mainScreen].bounds.size.width, 30)];
    
    [_telLabel setFrame:CGRectMake(10, self.fee.frame.size.height + 10 + self.fee.frame.origin.y, [UIScreen mainScreen].bounds.size.width, 20)];
    
    [_tel setFrame:CGRectMake(10, self.telLabel.frame.size.height + 10 + self.telLabel.frame.origin.y, [UIScreen mainScreen].bounds.size.width, 30)];
    
    [_webLabel setFrame:CGRectMake(10, self.tel.frame.size.height + 10 + self.tel.frame.origin.y, [UIScreen mainScreen].bounds.size.width, 20)];
    
    [_website setFrame:CGRectMake(10, self.webLabel.frame.size.height + 10 + self.webLabel.frame.origin.y, [UIScreen mainScreen].bounds.size.width, 30)];
    
}

@end
